package nl.hr.cmi.opleiding.hodexbridge;

import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import nl.hr.cmi.opleiding.hodexbridge.model.Opleidinginformatie;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.util.Log;

public class DomFeedParser extends BaseFeedParser {
	
	private static final String TAG = DomFeedParser.class.getSimpleName();

	public DomFeedParser(String feedUrl) {
		super(feedUrl);
	}

	public List<Opleidinginformatie> parse() {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		List<Opleidinginformatie> messages = new ArrayList<Opleidinginformatie>();
		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document dom = builder.parse(this.getInputStream());
			Element root = dom.getDocumentElement();
			NodeList items = root.getElementsByTagName(BaseFeedParser.PROGRAMSUMMARY);

			for (int i = 0; i < items.getLength(); i++) {
				Opleidinginformatie oi = new Opleidinginformatie();
				Node item = items.item(i);
				NodeList properties = item.getChildNodes();
				//item.toString();
				Log.d(TAG,item.toString());
				for (int j = 0; j < properties.getLength(); j++) {
					Node property = properties.item(j);
					
					//Log.d(TAG,property.getNod());
					//if(property.getNodeValue())
					// if (name.equalsIgnoreCase(TITLE)){
					// message.setTitle(property.getFirstChild().getNodeValue());
					// } else if (name.equalsIgnoreCase(LINK)){
					// message.setLink(property.getFirstChild().getNodeValue());
					// } else if (name.equalsIgnoreCase(DESCRIPTION)){
					// StringBuilder text = new StringBuilder();
					// NodeList chars = property.getChildNodes();
					// for (int k=0;k<chars.getLength();k++){
					// text.append(chars.item(k).getNodeValue());
					// }
					// message.setDescription(text.toString());
					// } else if (name.equalsIgnoreCase(PUB_DATE)){
					// message.setDate(property.getFirstChild().getNodeValue());
					// }
				}
				messages.add(oi);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return messages;
	}
}